home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
toogl
/
regexp.h
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
4KB
|
147 lines
/*
* Copyright (c) 1992, 1993 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the name of
* Silicon Graphics may not be used in any advertising or publicity relating
* to the software without the specific, prior written permission of
* Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF
* ANY KIND, EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION,
* ANY WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
* RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
* THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* version 1.6
* Regexp is a class that encapsulates the Regular expression
* stuff. Hopefully this means I can plug in different regexp
* libraries without the rest of my code needing to be changed.
* Written by Jim Morris, jegm@sgi.com
*/
#ifndef _REGEXP_H
#define _REGEXP_H
#include <iostream.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <assert.h>
#include <ctype.h>
#include "regex.h"
/*
* Note this is an inclusive range where it goes
* from start() to, and including, end()
*/
class Range
{
private:
int st, en;
public:
Range()
{
st=0; en= -1;
}
Range(int s, int e)
{
st= s; en= e;
}
int start(void) const { return st;}
int end(void) const { return en;}
int length(void) const { return (en-st)+1;}
};
class Regexp
{
public:
enum options {def=0, nocase=1};
private:
regexp *repat;
const char *target; // only used as a base address to get an offset
int res;
int iflg;
#ifndef __TURBOC__
void strlwr(char *s)
{
while(*s){
*s= tolower(*s);
s++;
}
}
#endif
public:
Regexp(const char *rege, int ifl= 0)
{
iflg= ifl;
if(iflg == nocase){ // lowercase fold
char *r= new char[strlen(rege)+1];
strcpy(r, rege);
strlwr(r);
if((repat=regcomp(r)) == NULL){
cerr << "regcomp() error" << endl;
exit(1);
}
delete [] r;
}else{
if((repat=regcomp (rege)) == NULL){
cerr << "regcomp() error" << endl;
exit(1);
}
}
}
~Regexp()
{
free(repat);
}
int match(const char *targ)
{
int res;
if(iflg == nocase){ // fold lowercase
char *r= new char[strlen(targ)+1];
strcpy(r, targ);
strlwr(r);
res= regexec(repat, r);
target= r; // looks bad but is really ok, really
delete [] r;
}else{
res= regexec(repat, targ);
target= targ;
}
return ((res == 0) ? 0 : 1);
}
int groups(void) const
{
int res= 0;
for (int i=0; i<NSUBEXP; i++) {
if(repat->startp[i] == NULL) break;
res++;
}
return res;
}
Range getgroup(int n) const
{
assert(n < NSUBEXP);
return Range((int)(repat->startp[n] - (char *)target),
(int)(repat->endp[n] - (char *)target) - 1);
}
};
#endif